var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate_list:[],
    project_list:[],
    symptom_list:[],
    keyword:"",
    flag:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.cate()
    this.project()
    this.symptom()
    this.package()
  },

  info(e){
    wx.navigateTo({
      url: '/pages/info/index?id='+e.currentTarget.dataset.id,
    })
  },

  insert_cart(e){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/insert_cart",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{user_id:getApp().globalData.userInfo.id,id:e.currentTarget.dataset.id,project_id:e.currentTarget.dataset.projectid},
      success: function(re) {
        wx.showToast({
          title: '加入购物车成功',
        })
        app.cart_count()
      }
    })
  },

  keyword_action(e){
    this.setData({
      keyword:e.detail.value
    })
  },

  search_action(){
    if(this.data.keyword!=''){
      this.setData({
        flag:1
      })
    }else{
      this.setData({
        flag:0
      })
    }
    this.package()
  },

  package(){
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/package",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{keyword:that.data.keyword},
      success: function(re) {
        wx.hideLoading()
        that.setData({
          package_list:re.data
        })
      }
    })
  },

  cate(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/cate",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success: function(re) {
        console.log(re)
        that.setData({
          cate_list:re.data
        })
      }
    })
  },
  symptom(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/symptom",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success: function(re) {
        console.log(re)
        that.setData({
          symptom_list:re.data
        })
      }
    })
  },
  project(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/project",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success: function(re) {
        console.log(re)
        that.setData({
          project_list:re.data
        })
      }
    })
  },

  daohang_action(e){
    console.log(e)
    this.setData({
      keyword:e.currentTarget.dataset.title
    })
    this.search_action()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})